"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _utils = require("../utils");

var _utils2 = require("./utils");

var _icon = _interopRequireDefault(require("../icon"));

var _image = _interopRequireDefault(require("../image"));

var _imagePreview = _interopRequireDefault(require("../image-preview"));

var _createNamespace = (0, _utils.createNamespace)('uploader'),
    createComponent = _createNamespace[0],
    bem = _createNamespace[1];

var _default2 = createComponent({
    inheritAttrs: false,
    model: {
        prop: 'fileList'
    },
    props: {
        disabled: Boolean,
        uploadText: String,
        afterRead: Function,
        beforeRead: Function,
        beforeDelete: Function,
        previewSize: [Number, String],
        name: {
            type: [Number, String],
            default: ''
        },
        accept: {
            type: String,
            default: 'image/*'
        },
        fileList: {
            type: Array,
            default: function _default() {
                return [];
            }
        },
        maxSize: {
            type: Number,
            default: Number.MAX_VALUE
        },
        maxCount: {
            type: Number,
            default: Number.MAX_VALUE
        },
        deletable: {
            type: Boolean,
            default: true
        },
        previewImage: {
            type: Boolean,
            default: true
        },
        previewFullImage: {
            type: Boolean,
            default: true
        },
        imageFit: {
            type: String,
            default: 'cover'
        },
        resultType: {
            type: String,
            default: 'dataUrl'
        }
    },
    computed: {
        previewSizeWithUnit: function previewSizeWithUnit() {
            return (0, _utils.addUnit)(this.previewSize);
        }
    },
    methods: {
        getDetail: function getDetail(index) {
            if (index === void 0) {
                index = this.fileList.length;
            }

            return {
                name: this.name,
                index: index
            };
        },
        onChange: function onChange(event) {
            var _this = this;

            var files = event.target.files;

            if (this.disabled || !files.length) {
                return;
            }

            files = files.length === 1 ? files[0] : [].slice.call(files);

            if (this.beforeRead) {
                var response = this.beforeRead(files, this.getDetail());

                if (!response) {
                    this.resetInput();
                    return;
                }

                if (response.then) {
                    response.then(function () {
                        _this.readFile(files);
                    }).catch(this.resetInput);
                    return;
                }
            }

            this.readFile(files);
        },
        readFile: function readFile(files) {
            var _this2 = this;

            var oversize = (0, _utils2.isOversize)(files, this.maxSize);

            if (Array.isArray(files)) {
                var maxCount = this.maxCount - this.fileList.length;

                if (files.length > maxCount) {
                    files = files.slice(0, maxCount);
                }

                Promise.all(files.map(function (file) {
                    return (0, _utils2.readFile)(file, _this2.resultType);
                })).then(function (contents) {
                    var fileList = files.map(function (file, index) {
                        var result = {
                            file: file
                        };

                        if (contents[index]) {
                            result.content = contents[index];
                        }

                        return result;
                    });

                    _this2.onAfterRead(fileList, oversize);
                });
            } else {
                (0, _utils2.readFile)(files, this.resultType).then(function (content) {
                    var result = {
                        file: files
                    };

                    if (content) {
                        result.content = content;
                    }

                    _this2.onAfterRead(result, oversize);
                });
            }
        },
        onAfterRead: function onAfterRead(files, oversize) {
            this.resetInput();

            if (oversize) {
                this.$emit('oversize', files, this.getDetail());
                return;
            }

            this.$emit('input', [].concat(this.fileList, (0, _utils2.toArray)(files)));

            if (this.afterRead) {
                this.afterRead(files, this.getDetail());
            }
        },
        onDelete: function onDelete(file, index) {
            var _this3 = this;

            if (this.beforeDelete) {
                var response = this.beforeDelete(file, this.getDetail(index));

                if (!response) {
                    return;
                }

                if (response.then) {
                    response.then(function () {
                        _this3.deleteFile(file, index);
                    }).catch(_utils.noop);
                    return;
                }
            }

            this.deleteFile(file, index);
        },
        deleteFile: function deleteFile(file, index) {
            var fileList = this.fileList.slice(0);
            fileList.splice(index, 1);
            this.$emit('input', fileList);
            this.$emit('delete', file, this.getDetail(index));
        },
        resetInput: function resetInput() {
            /* istanbul ignore else */
            if (this.$refs.input) {
                this.$refs.input.value = '';
            }
        },
        onPreviewImage: function onPreviewImage(item) {
            var _this4 = this;

            if (!this.previewFullImage) {
                return;
            }

            var imageFiles = this.fileList.filter(function (item) {
                return (0, _utils2.isImageFile)(item);
            });
            var imageContents = imageFiles.map(function (item) {
                return item.content || item.url;
            });
            this.imagePreview = (0, _imagePreview.default)({
                images: imageContents,
                closeOnPopstate: true,
                startPosition: imageFiles.indexOf(item),
                onClose: function onClose() {
                    _this4.$emit('close-preview');
                }
            });
        },
        // @exposed-api
        closeImagePreview: function closeImagePreview() {
            if (this.imagePreview) {
                this.imagePreview.close();
            }
        },
        genPreviewItem: function genPreviewItem(item, index) {
            var _this5 = this;

            var h = this.$createElement;
            var DeleteIcon = this.deletable && h(_icon.default, {
                "attrs": {
                    "name": "clear"
                },
                "class": bem('preview-delete'),
                "on": {
                    "click": function click(event) {
                        event.stopPropagation();

                        _this5.onDelete(item, index);
                    }
                }
            });
            var Preview = (0, _utils2.isImageFile)(item) ? h(_image.default, {
                "attrs": {
                    "fit": this.imageFit,
                    "src": item.content || item.url,
                    "width": this.previewSize,
                    "height": this.previewSize,
                    "radius": 4
                },
                "class": bem('preview-image'),
                "on": {
                    "click": function click() {
                        _this5.onPreviewImage(item);
                    }
                }
            }) : h("div", {
                "class": bem('file'),
                "style": {
                    width: this.previewSizeWithUnit,
                    height: this.previewSizeWithUnit
                }
            }, [h(_icon.default, {
                "class": bem('file-icon'),
                "attrs": {
                    "name": "description"
                }
            }), h("div", {
                "class": [bem('file-name'), 'van-ellipsis']
            }, [item.file ? item.file.name : item.url])]);
            return h("div", {
                "class": bem('preview'),
                "on": {
                    "click": function click() {
                        _this5.$emit('click-preview', item, _this5.getDetail(index));
                    }
                }
            }, [Preview, DeleteIcon]);
        },
        genPreviewList: function genPreviewList() {
            if (this.previewImage) {
                return this.fileList.map(this.genPreviewItem);
            }
        },
        genUpload: function genUpload() {
            var h = this.$createElement;

            if (this.fileList.length >= this.maxCount) {
                return;
            }

            var slot = this.slots();
            var Input = h("input", {
                "attrs": (0, _extends2.default)({}, this.$attrs, {
                    "type": "file",
                    "accept": this.accept,
                    "disabled": this.disabled
                }),
                "ref": "input",
                "class": bem('input'),
                "on": {
                    "change": this.onChange
                }
            });

            if (slot) {
                return h("div", {
                    "class": bem('input-wrapper')
                }, [slot, Input]);
            }

            var style;

            if (this.previewSize) {
                var size = this.previewSizeWithUnit;
                style = {
                    width: size,
                    height: size
                };
            }

            return h("div", {
                "class": bem('upload'),
                "style": style
            }, [h(_icon.default, {
                "attrs": {
                    "name": "plus"
                },
                "class": bem('upload-icon')
            }), this.uploadText && h("span", {
                "class": bem('upload-text')
            }, [this.uploadText]), Input]);
        }
    },
    render: function render() {
        var h = arguments[0];
        return h("div", {
            "class": bem()
        }, [h("div", {
            "class": bem('wrapper')
        }, [this.genPreviewList(), this.genUpload()])]);
    }
});

exports.default = _default2;